package com.itheima.controller;

import com.itheima.dto.AdvertiseDto;

import com.itheima.pojo.SmsHomeAdvertise;
import com.itheima.service.SmsHomeAdvertiseService;
import com.itheima.vo.PageResultVo;
import com.itheima.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/home/advertise")
public class SmsHomeAdvertiseController {
    @Autowired
    private SmsHomeAdvertiseService smsHomeAdvertiseService;

    /**
     * 添加广告
     *
     * @param advertiseDto
     * @return
     */
    @PostMapping("create")
    public ResponseEntity create(@RequestBody SmsHomeAdvertise advertiseDto) {
        smsHomeAdvertiseService.create(advertiseDto);
        return ResponseEntity.ok(Result.success(null));

    }

    /**
     * 删除
     * @param ids
     * @return
     */
    @PostMapping("delete")
    public ResponseEntity delete(Integer [] ids) {
        smsHomeAdvertiseService.delete(ids);
        return ResponseEntity.ok(Result.success(null));
    }

    /**
     * 分页查询广告
     * @param
     * @return
     */
    @GetMapping("list")
    public ResponseEntity list(@RequestParam(defaultValue = "1") Integer pageNum,
                               @RequestParam(defaultValue = "5") Integer pageSize,
                               String endTime,
                               String name,
                               Integer type) {
        PageResultVo<SmsHomeAdvertise> pageResultVo=smsHomeAdvertiseService.list(pageNum,pageSize,endTime,name,type);
        return ResponseEntity.ok(Result.success(pageResultVo));
    }


    /**
     * 修改上下线状态
     * @param id
     * @param status
     * @return
     */
    @PostMapping("/update/status/{id}")
    public ResponseEntity updateStatus(@PathVariable("id") Integer id,Integer status){
        smsHomeAdvertiseService.updateStatus(id,status);
        return ResponseEntity.ok(Result.success(null));
    }


   // /home/advertise/update/{id}

    /**
     * 根据广告的id 修改
     * @param
     * @param
     * @return
     */
   @PostMapping("/update/{id}")
   public ResponseEntity update(@RequestBody SmsHomeAdvertise smsHomeAdvertise,
                                @PathVariable("id") Integer id){
       if(id!=null){
           smsHomeAdvertise.setId(id.longValue());
       }
       smsHomeAdvertiseService.update(smsHomeAdvertise);
       return ResponseEntity.ok(Result.success(null));
   }


    /**
     * 根据 id获取广告详情
     * @param id
     * @return
     */
    ///home/advertise/{id}
    @GetMapping("{id}")
    public ResponseEntity getById(@PathVariable("id") Integer id){
        SmsHomeAdvertise smsHomeAdvertise= smsHomeAdvertiseService.findById(id);
        return ResponseEntity.ok(Result.success(smsHomeAdvertise));
    }

}
